package com.sgb.member.ui.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.SparseArray;
import android.view.View;

import androidx.appcompat.widget.AppCompatEditText;
import androidx.recyclerview.widget.RecyclerView;

import com.sgb.member.BR;
import com.sgb.member.R;
import com.sgb.member.databinding.ItemMemberJoinThresholdBinding;
import com.sgb.member.entity.ThresholdParamBean;
import com.sgb.member.ui.activity.MemberTransferActivity;
import com.sgb.member.util.ChooseFileUtil;
import com.sgb.member.weight.AppendFieldsView;
import com.sgb.member.weight.SimpleResult;
import com.sgb.member.weight.UploadFileBean;
import com.sgb.member.weight.UploadFileLayout;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;
import com.swgk.core.util.MToast;
import com.swgk.core.util.StringUtil;

import java.util.ArrayList;
import java.util.List;

public class MemberJoinThresholdAdapter extends BaseRecycleAdapter<ThresholdParamBean> {

    private int curIndex;

    public MemberJoinThresholdAdapter(Context context, List<ThresholdParamBean> datas) {
        super(context, R.layout.item_member_join_threshold, datas);
    }

    @Override
    public void onBindViewHolder(ItemViewHolder holder, @SuppressLint("RecyclerView") int position) {
        super.onBindViewHolder(holder, position);
        ThresholdParamBean s = datas.get(position);
        ItemMemberJoinThresholdBinding binding = (ItemMemberJoinThresholdBinding) holder.getmBinding();
        binding.etRequiredContent.setTitle(s.getRequiredContent());
        editTextChanged(binding.etRequiredContent.getEditText(), getDatas().get(holder.getPosition()), holder.getPosition());
        binding.etRequiredContent.setContentHint("请输入");
        // 是否证明文件1需要2不需要
        if (s.getIsSupportDoc() == 1) {
            binding.uploadFileLayout.setVisibility(View.VISIBLE);
        } else {
            binding.uploadFileLayout.setVisibility(View.GONE);
        }
        binding.uploadFileLayout.setData(s.getTempBeans(), new SimpleResult<Integer>() {
            @SuppressLint("NotifyDataSetChanged")
            @Override
            public void onResult(Integer integer, Object... objects) {
                int pos = integer;
                List<UploadFileBean> tempBeans = datas.get(position).getTempBeans();
                List<UploadFileBean> beans = new ArrayList<>();
                for (int i = 0; i < tempBeans.size(); i++) {
                    if (i != pos) {
                        beans.add(tempBeans.get(i));
                    }
                }
                datas.get(position).setTempBeans(beans);
                notifyDataSetChanged();
            }
        });
        binding.uploadFileLayout.setSimpleResult(new SimpleResult<UploadFileLayout>() {
            @Override
            public void onResult(UploadFileLayout o, Object... objects) {
                curIndex = position;
            }
        });
    }

    @Override
    public void convert(ItemViewHolder holder, ThresholdParamBean s) {
        holder.setBinding(BR.viewModel, s);
    }

    private void editTextChanged(AppCompatEditText editText, ThresholdParamBean field, int pos) {
        if (editText.getTag() != null) {
            editText.removeTextChangedListener((TextWatcher) editText.getTag());
            editText.clearFocus();
        }
        editText.setText(field.getContent());
        TextWatcher watcher = new TextWatcher() {

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                getDatas().get(pos).setContent(s.toString());
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        };
        editText.addTextChangedListener(watcher);
        editText.setTag(watcher);
    }

    public void handleFileChoose(Context context, Intent data) {
        ChooseFileUtil.handleFileChoose(context, data, new SimpleResult<UploadFileBean>() {
            @Override
            public void onResult(UploadFileBean uploadFileBean, Object... objects) {
                List<UploadFileBean> beans = datas.get(curIndex).getTempBeans();
                if (beans != null && beans.size() > 0) {
                    for (UploadFileBean bean : beans) {
                        if (bean.getFilePath().equals(uploadFileBean.getFilePath())) {
                            MToast.showToast(context, "该文件已上传！");
                            return;
                        }
                    }
                }
                if (simpleResult != null) {
                    simpleResult.onResult(uploadFileBean, new Object());
                }
            }
        });
    }

    @SuppressLint("NotifyDataSetChanged")
    public void addFileData(UploadFileBean uploadFileBean) {
        List<UploadFileBean> beans = datas.get(curIndex).getTempBeans();
        if (beans != null && beans.size() > 0) {
            datas.get(curIndex).getTempBeans().add(uploadFileBean);
        } else {
            List<UploadFileBean> uploadFileBeans = new ArrayList<>();
            uploadFileBeans.add(uploadFileBean);
            datas.get(curIndex).setTempBeans(uploadFileBeans);
        }
        notifyDataSetChanged();
    }

    public List<ThresholdParamBean> getThresholdParams() {
        if (datas != null) {
            List<ThresholdParamBean> paramBeans = new ArrayList<>();
            for (int i = 0; i < datas.size(); i++) {
                ThresholdParamBean data = datas.get(i);
                data.setTitle(data.getRequiredContent());
                data.setThresholdNo(data.getNo());
                if (data.getTempBeans() != null) {
                    List<ThresholdParamBean.FileListBean> paths = new ArrayList<>();
                    for (UploadFileBean datum : data.getTempBeans()) {
                        paths.add(new ThresholdParamBean.FileListBean(datum.getUrlPath(), datum.getFileName()));
                    }
                    data.setFileList(paths);
                }
                if (StringUtil.isEmpty(data.getContent())) {
                    MToast.showToast(context, "请填写" + data.getRequiredContent());
                    return null;
                }
                paramBeans.add(data);
            }
            return paramBeans;
        }
        return null;
    }

    private SimpleResult<UploadFileBean> simpleResult;

    public void setSimpleResult(SimpleResult<UploadFileBean> simpleResult) {
        this.simpleResult = simpleResult;
    }
}
